<?php // $Id: format.php,v 1.74.2.6 2008/12/10 06:05:27 dongsheng Exp $
      // Display the whole course as "weeks" made of of modules
      // Included from "view.php"

    require_once($CFG->libdir.'/ajax/ajaxlib.php');
    require_once($CFG->dirroot.'/course/format/aformat/newaforumform.php');
    require_once($CFG->dirroot.'/course/format/aformat/newaforumthemeform.php');
    require_once($CFG->dirroot.'/course/format/aformat/newaforumpostform.php');
    require_once($CFG->dirroot.'/course/format/aformat/afreport.php');
    require_once($CFG->dirroot.'/course/format/aformat/afcollection.php');
    
    $week = optional_param('week', -1, PARAM_INT);

    // Bounds for block widths
    // more flexible for theme designers taken from theme config.php
    $lmin = (empty($THEME->block_l_min_width)) ? 100 : $THEME->block_l_min_width;
    $lmax = (empty($THEME->block_l_max_width)) ? 210 : $THEME->block_l_max_width;
    $rmin = (empty($THEME->block_r_min_width)) ? 100 : $THEME->block_r_min_width;
    $rmax = (empty($THEME->block_r_max_width)) ? 210 : $THEME->block_r_max_width;

    define('BLOCK_L_MIN_WIDTH', $lmin);
    define('BLOCK_L_MAX_WIDTH', $lmax);
    define('BLOCK_R_MIN_WIDTH', $rmin);
    define('BLOCK_R_MAX_WIDTH', $rmax);

    $preferred_width_left  = bounded_number(BLOCK_L_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
                                            BLOCK_L_MAX_WIDTH);
    $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
                                            BLOCK_R_MAX_WIDTH);

    if ($week != -1) {
        $displaysection = course_set_display($course->id, $week);
    } else {
        if (isset($USER->display[$course->id])) {
            $displaysection = $USER->display[$course->id];
        } else {
            $displaysection = course_set_display($course->id, 0);
        }
    }

    $streditsummary  = get_string('editsummary');
    $stradd          = get_string('add');
    $stractivities   = get_string('activities');
    $strshowallweeks = get_string('showallweeks');
    $strweek         = get_string('week');
    $strgroups       = get_string('groups');
    $strgroupmy      = get_string('groupmy');
    $editing         = $PAGE->user_is_editing();

    if ($editing) {
        $strstudents = moodle_strtolower($course->students);
        $strweekhide = get_string('weekhide', '', $strstudents);
        $strweekshow = get_string('weekshow', '', $strstudents);
        $strmoveup   = get_string('moveup');
        $strmovedown = get_string('movedown');
    }

    $context = get_context_instance(CONTEXT_COURSE, $course->id);
/// Layout the whole page as three big columns.
    echo '<table id="layout-table" cellspacing="0" summary="'.get_string('layouttable').'"><tr>';
    $lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
    foreach ($lt as $column) {
        switch ($column) {
            case 'left':

/// The left column ...

    if (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $editing) {
        echo '<td style="width:'.$preferred_width_left.'px" id="left-column">';

        print_container_start();
        blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
        print_container_end();

        echo '</td>';
    }
            break;
            case 'middle':
/// Start main column
    echo '<td id="middle-column">';

    print_container_start();

    echo skip_main_destination();

    print_heading_block(get_string('formataformat','format_aformat'));

    echo '<table class="weeks" width="100%" summary="'.get_string('layouttable').'">';
    
/// If currently moving a file then show the current clipboard
    if (ismoving($course->id)) {
        $stractivityclipboard = strip_tags(get_string('activityclipboard', '', addslashes($USER->activitycopyname)));
        $strcancel= get_string('cancel');
        echo '<tr class="clipboard">';
        echo '<td colspan="3">';
        echo $stractivityclipboard.'&nbsp;&nbsp;(<a href="mod.php?cancelcopy=true&amp;sesskey='.$USER->sesskey.'">'.$strcancel.'</a>)';
        echo '</td>';
        echo '</tr>';
    }

/// Print Section 0 with general activities

    $section = 0;
    $thissection = $sections[$section];

    if ($thissection->summary or $thissection->sequence or isediting($course->id)) {
        echo '<tr id="section-0" class="section main">';
        echo '<td class="left side">&nbsp;</td>';
        echo '<td class="content">';

        echo '<div class="summary">';
        $summaryformatoptions->noclean = true;
        echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions);

        if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
            echo '<a title="'.$streditsummary.'" '.
                 ' href="editsection.php?id='.$thissection->id.'"><img src="'.$CFG->pixpath.'/t/edit.gif" '.
                 'class="iconsmall edit" alt="'.$streditsummary.'" /></a><br /><br />';
        }
        echo '</div>';

        print_section($course, $thissection, $mods, $modnamesused);

        if (isediting($course->id)) {
            print_section_add_menus($course, $section, $modnames);
        }

        echo '</td>';
        echo '<td class="right side">&nbsp;</td>';
        echo '</tr>';
        if(isset($_GET[aformatcollection])) {
            // TEST COLLECTION
            echo '<tr>';
            echo '<td class="content" colspan="3">';
            $collection = new afcollection($_GET[aformatcollection],$course->id);
            $collection->collections();
            echo '</td>';
            echo '</tr>';
            // END TEST
        }
        // New AForum Section
        if ((!isset($_GET['aformatforumid'])||!(count_records('aformat_forum', 'id', $_GET['aformatforumid'])>0))&&(!isset($_GET['aformatforumthemeid'])||!(count_records('aformat_forum_themes','id',$_GET['aformatforumthemeid'])>0))) {
            if (isediting($course->id)) {
                // Delete aforum
                if (isset($_GET['aformatforumdelid'])) {
                    delete_records('aformat_forum', 'id', $_GET['aformatforumdelid']);
                    $aforumthemes = get_records('aformat_forum_themes','forumid',$_GET['aformatforumdelid']);
                    foreach ($aforumthemes as $aftRecord) {
                        delete_records('aformat_forum_posts', 'themeid', $aftRecord->id);
                    }
                    delete_records('aformat_forum_themes', 'forumid', $_GET['aformatforumdelid']);
                }
                echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                echo '<tr class="section main">';
                
                echo '<td colspan="3" class="content">';
                echo '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.get_string('newaforum','format_aformat').'</a>';
                echo '<div>';
                if (!isset($_GET['aformatforumeditid'])) {
                    $newafform = new newaforumform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey");
                    $newafform->add_action_buttons($cancel = true, $submitlabel=null);

                    $newafform->display();
                    if ($newafform->is_cancelled()) {

                    } else if ($fromform=$newafform->get_data()) {
                        $afRecord = new object();
                        $afRecord->name = $fromform->aforumname;
                        $afRecord->description = $fromform->aforumdescription;
                        $afRecord->course = $course->id;
                        insert_record('aformat_forum', $afRecord);
                    }
                }
                
                echo '</div>';
                echo '</td>';
                
                echo '</tr>';
            }
            $aforums = get_records('aformat_forum', 'course', $course->id);
            foreach($aforums as $afRecord) {
                echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                echo '<tr class="section main">';
                
                echo '<td style="border:solid silver 1px" colspan="3" class="content">';
                if (isset($_GET['aformatforumeditid'])&&isediting($course->id)&&($afRecord->id==$_GET['aformatforumeditid'])) {
                    $editafform = new newaforumform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey&aformatforumeditid=$afRecord->id&aformatforumedited=true#aformatforum'.$afRecord->id.'");
                    $editafform->add_action_buttons($cancel = true, $submitlabel=null);
                    $editafform->set_data(array ('aforumname' => $afRecord->name, 'aforumdescription' => $afRecord->description));
                    
                    if ($editafform->is_cancelled()) {

                    } else if ($fromform=$editafform->get_data()) {
                        $afRecord = new object();
                        $afRecord->id = $_GET['aformatforumeditid'];
                        $afRecord->name = $fromform->aforumname;
                        $afRecord->description = $fromform->aforumdescription;
                        $afRecord->course = $course->id;
                        update_record('aformat_forum', $afRecord);
                    }
                }
                echo '<div><strong>';
                echo '<a name="aformatforum'.$afRecord->id.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$afRecord->id.'">'.$afRecord->name.'</a>';
                echo '</strong></div>';
                if ($afRecord->description!='') {
                    echo '<br />';
                    echo '<div>';
                    echo $afRecord->description;
                    echo '</div>';
                }
                if (isset($_GET['aformatforumeditid'])&&isediting($course->id)&&($afRecord->id==$_GET['aformatforumeditid'])) {
                    if (!isset($_GET['aformatforumedited'])) {
                        $editafform->display();
                    }
                }
                if (isediting($course->id)) {
                    echo '<div align="right">';
                    echo '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumeditid='.$afRecord->id.'#aformatforum'.$afRecord->id.'">edit</a> | ';
                    echo '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumdelid='.$afRecord->id.'">delete</a>';
                    echo '</div>';
                }
                echo '</td>';
                
                echo '</tr>';
            }
        }
        
        // New theme section
        if (isset($_GET['aformatforumid'])&&(count_records('aformat_forum', 'id', $_GET['aformatforumid'])>0)) {
            $aformatforumid = $_GET['aformatforumid'];
            $aforum = get_record('aformat_forum', 'id', $aformatforumid);
            echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
            echo '<tr class="section main">';
            echo '<td colspan="3" style="border:solid silver 1px" class="content">';
            echo "<strong>$aforum->name</strong>";
            echo '</td>';
            echo '</tr>';
            // Navigation inside the course
            echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
            echo '<tr class="section main">';
            
            echo '<td colspan="3" style="border:solid silver 1px" class="content">';
            echo '[<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">Forums</a>]';
            if($_GET[aformatreport]){
                echo ' [<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aformatforumid.'">Themes</a>]';
            } else {
                echo ' [<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aformatforumid.'&aformatreport=true">Report</a>]';
            }
            echo '</td>';
            
            echo '</tr>';
            
            if($_GET[aformatreport]){
                // TEST REPORT
                echo '<tr>';
                echo '<td colspan="3">';
                $rep = new afreport($_GET['aformatforumid'], $course->id);
                echo $rep->reportTable();
                echo '</td>';
                echo '</tr>';
                // END TEST
            } else {
                if (isediting($course->id)) {
                    // Delete aforumtheme
                    if (isset($_GET['aformatforumthemedelid'])) {
                        delete_records('aformat_forum_posts', 'themeid', $_GET['aformatforumthemedelid']);
                        delete_records('aformat_forum_themes', 'id', $_GET['aformatforumthemedelid']);
                    }
                    echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                    echo '<tr class="section main">';

                    echo '<td colspan="3" style="border:solid silver 1px" class="content">';
                    echo '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aformatforumid.'">'.get_string('newtheme','format_aformat').'</a>';
                    echo '<div>';
                    if (!isset($_GET['aformatforumthemeeditid'])) {
                        $newaftform = new newaforumthemeform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey&aformatforumid=$aformatforumid");
                        $newaftform->add_action_buttons($cancel = true, $submitlabel=null);

                        $newaftform->display();
                        if ($newaftform->is_cancelled()) {

                        } else if ($fromform=$newaftform->get_data()) {
                            $aftRecord = new object();
                            $aftRecord->forumid = $aformatforumid;
                            $aftRecord->subject = $fromform->aforumthemesubject;
                            $aftRecord->description = $fromform->aforumthemedescription;
                            $aftRecord->datestart = $fromform->aforumthemestartdate;
                            $aftRecord->image = $fromform->aforumthemeimage;
                            $aftRecord->imagewidth = $fromform->aforumthemeimgwidth;
                            $aftRecord->imageheight = $fromform->aforumthemeimgheight;
                            $aftRecord->postimagewidth = $fromform->aforumthemepostimgwidth;
                            $aftRecord->postimageheight = $fromform->aforumthemepostimgheight;
                            $aftRecord->subjectbgcolor = $fromform->aforumthemesubjbgcolor;
                            $aftRecord->subjectforecolor = $fromform->aforumthemesubjforecolor;
                            $aftRecord->expireafterdays = $fromform->aforumthemeexpire;
                            $aftRecord->maxpoints = $fromform->aforumthememaxp;
                            $aftRecord->penalpointsafterdays = $fromform->aforumthemeprule;
                            $aftRecord->postssorting = $fromform->aforumthemesorting;

                            insert_record('aformat_forum_themes', $aftRecord);
                        }
                    }

                    echo '</div>';
                    echo '</td>';

                    echo '</tr>';
                }
                $aforumthemes = get_records('aformat_forum_themes','forumid',$aformatforumid);
                foreach ($aforumthemes as $aftRecord) {
                    echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                    echo '<tr class="section main">';

                    echo '<td colspan="3" style="border:solid silver 1px" class="content">';
                    if (isset($_GET['aformatforumthemeeditid'])&&isediting($course->id)&&($aftRecord->id==$_GET['aformatforumthemeeditid'])) {
                        $editaftform = new newaforumthemeform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey&aformatforumid=$aformatforumid&aformatforumthemeeditid=$aftRecord->id&aformatthemeedited=true#aformatforumtheme$aftRecord->id");
                        $editaftform->add_action_buttons($cancel = true, $submitlabel=null);
                        $editaftform->set_data(
                                array (
                                    'aforumthemesubject' => $aftRecord->subject,
                                    'aforumthemedescription' => $aftRecord->description,
                                    'aforumthemestartdate' => $aftRecord->datestart,
                                    'aforumthemeimage' => $aftRecord->image,
                                    'aforumthemeimgwidth' => $aftRecord->imagewidth,
                                    'aforumthemeimgheight' => $aftRecord->imageheight,
                                    'aforumthemepostimgwidth' => $aftRecord->postimagewidth,
                                    'aforumthemepostimgheight' => $aftRecord->postimageheight,
                                    'aforumthemesubjbgcolor' => $aftRecord->subjectbgcolor,
                                    'aforumthemesubjforecolor' => $aftRecord->subjectforecolor,
                                    'aforumthemeexpire' => $aftRecord->expireafterdays,
                                    'aforumthememaxp' => $aftRecord->maxpoints,
                                    'aforumthemeprule' => $aftRecord->penalpointsafterdays,
                                    'aforumthemesorting' => $aftRecord->postssorting,
                                )
                                );


                        if ($editaftform->is_cancelled()) {

                        } else if ($fromform=$editaftform->get_data()) {
                            $aftRecord = new object();
                            $aftRecord->id = $_GET['aformatforumthemeeditid'];
                            $aftRecord->forumid = $aformatforumid;
                            $aftRecord->subject = $fromform->aforumthemesubject;
                            $aftRecord->description = $fromform->aforumthemedescription;
                            $aftRecord->datestart = $fromform->aforumthemestartdate;
                            $aftRecord->image = $fromform->aforumthemeimage;
                            $aftRecord->imagewidth = $fromform->aforumthemeimgwidth;
                            $aftRecord->imageheight = $fromform->aforumthemeimgheight;
                            $aftRecord->postimagewidth = $fromform->aforumthemepostimgwidth;
                            $aftRecord->postimageheight = $fromform->aforumthemepostimgheight;
                            $aftRecord->subjectbgcolor = $fromform->aforumthemesubjbgcolor;
                            $aftRecord->subjectforecolor = $fromform->aforumthemesubjforecolor;
                            $aftRecord->expireafterdays = $fromform->aforumthemeexpire;
                            $aftRecord->maxpoints = $fromform->aforumthememaxp;
                            $aftRecord->penalpointsafterdays = $fromform->aforumthemeprule;
                            $aftRecord->postssorting = $fromform->aforumthemesorting;

                            update_record('aformat_forum_themes', $aftRecord);
                        }
                    }
                    echo '<table width="100%">';
                    echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'"><td colspan="2">';
                    echo '<a name="aformatforumtheme'.$aftRecord->id.'" style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$aftRecord->id.'">'.$aftRecord->subject.' ('.date('d/M/Y',$aftRecord->datestart).')</a>';
                    echo '</td></tr>';
                    echo '<div>';

                    echo '<tr><td>'.$aftRecord->description.'</td>';
                    if ($aftRecord->image!='') {
                        echo '<td align="right" valign="middle" width="'.$aftRecord->imagewidth.'px"><img border="0" width="'.$aftRecord->imagewidth.'" height="'.$aftRecord->imageheight.'" src="'.$CFG->wwwroot.'/file.php/'.$course->id.'/'.$aftRecord->image.'" /></td>';
                    }
                    echo '</tr>';
                    echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'">';
                    $aforumthemepostscount = count_records('aformat_forum_posts','themeid',$aftRecord->id);
                    $aftDays = time()-$aftRecord->datestart;
                    $aftDaysFormated=intval((($aftDays/60)/60)/24);
                    $aforumthemestatus = 'active';
                    if (($aftDaysFormated>$aftRecord->expireafterdays)) {
                        $aforumthemestatus = 'concluded';
                    }
                    echo '<td align="left">posts: '.$aforumthemepostscount.' | status: '.$aforumthemestatus.'</td>';
                    echo '<td align="right">';
                    if (isediting($course->id)) {
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aftRecord->forumid.'&aformatforumthemeeditid='.$aftRecord->id.'">edit</a>';
                        echo ' | ';
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aftRecord->forumid.'&aformatforumthemedelid='.$aftRecord->id.'">delete</a>';
                    }
                    echo '</td></tr>';
                    echo '</table>';

                    if (isset($_GET['aformatforumthemeeditid'])&&isediting($course->id)&&($aftRecord->id==$_GET['aformatforumthemeeditid'])) {
                        if (!isset($_GET['aformatthemeedited'])) {
                            $editaftform->display();
                        }
                    }
                    echo '</td>';

                    echo '</tr>';
                }
            }
        }
        // New post section
        if (isset($_GET['aformatforumthemeid'])&&(count_records('aformat_forum_themes','id',$_GET['aformatforumthemeid'])>0)) {
            // Delete aforumpost
            if (isset($_GET['aformatforumpostdelid'])) {
                $afpRecord = get_record('aformat_forum_posts', 'id', $_GET['aformatforumpostdelid']);
                if ($afpRecord->userid==$USER->id||isediting($course->id)) {
                    delete_records('aformat_forum_posts', 'id', $_GET['aformatforumpostdelid']);
                }
            }
            $aformatforumthemeid = $_GET['aformatforumthemeid'];
            $aftRecord = get_record('aformat_forum_themes','id',$aformatforumthemeid);
            $aftDays = time()-$aftRecord->datestart;
            $aftDaysFormated=intval((($aftDays/60)/60)/24);
            $aftConcluded=!($aftDaysFormated<=$aftRecord->expireafterdays);
            // Navigation inside the course
            echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
            echo '<tr class="section main">';
            
            echo '<td colspan="3" style="border:solid silver 1px" class="content">';
            echo '[<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">Forums</a>] [<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumid='.$aftRecord->forumid.'">Themes</a>]';
            echo '</td>';
            
            echo '</tr>';
            if (isediting($course->id)&&!isset($_GET['aformatforumposteditid'])||(isset($_GET['aformatforumpostpost'])&&!$aftConcluded&&isstudent($course->id))) {
                echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                echo '<tr class="section main">';
                
                echo '<td colspan="3" style="border:solid silver 1px" class="content">';
                echo get_string('newpost','format_aformat');
                echo '<div>';
                $afpFormLADD = '';
                if (isset($_GET['aformatforumpostpost'])) {
                    $afpFormLADD = '&aformatforumpostpost=true';
                    if (isset($_GET['aformatforumpostreply'])) {
                        $afpFormLADD .= '&aformatforumpostreply='.$_GET['aformatforumpostreply'];
                    }
                }
                $newafpform = new newaforumpostform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey&aformatforumthemeid=$aformatforumthemeid$afpFormLADD", $course);
                $newafpform->add_action_buttons($cancel = true, $submitlabel=null);

                $newafpform->display();
                if ($newafpform->is_cancelled()) {

                } else if ($fromform=$newafpform->get_data()) {
                    $afpRecord = new object();
                    $afpRecord->post = $fromform->aforumpostpost;
                    
                    $afpRecord->image = $newafpform->get_new_filename();
                    $afpRecord->themeid = $aformatforumthemeid;
                    $afpRecord->userid = $USER->id;
                    $afpRecord->posted = time();
                    if (isset($_GET['aformatforumpostreply'])) {
                        $afpRecord->postid = $_GET['aformatforumpostreply'];
                    }
                    
                    $rtid = insert_record('aformat_forum_posts', $afpRecord);
                    $aforumsavedir = "$course->id/aformatdata/$USER->id/$rtid";
                    $newafpform->save_files($aforumsavedir);
                    
                    //echo $newafpform->get_errors();
                }
                echo '</div>';
                echo '</td>';
                
                echo '</tr>';
            }
            // Edit post
            if (isset($_GET['aformatforumposteditid'])) {
                $afpRecord = get_record('aformat_forum_posts', 'id', $_GET['aformatforumposteditid']);
                if (isediting($course->id)||$USER->id==$afpRecord->userid) {
                    echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                    echo '<tr class="section main">';
                    
                    echo '<td colspan="3" style="border:solid silver 1px" class="content">';
                    echo '<div>';
                    $editafpform = new newaforumpostform("$CFG->wwwroot/course/view.php?id=$course->id&sesskey=$USER->sesskey&aformatforumthemeid=$aformatforumthemeid&aformatforumposteditid=$afpRecord->id", $course);
                    $editafpform->add_action_buttons($cancel = true, $submitlabel=null);
                    $editafpform->set_data(array('aforumpostpost' => $afpRecord->post));
                    $editafpform->display();
                    if ($editafpform->is_cancelled()) {

                    } else if ($fromform=$editafpform->get_data()) {
                        $afpRecord->post = $fromform->aforumpostpost;
                        if (isset($_FILES['aforumpostimage']['name'])&&$_FILES['aforumpostimage']['name']!='') {
                            //$afpRecord->image = $_FILES['aforumpostimage']['name'];
                            $afpRecord->image = $editafpform->get_new_filename();
                            $aforumsavedir = "$course->id/aformatdata/$afpRecord->userid/$afpRecord->id";
                            $editafpform->save_files($aforumsavedir);
                        }
                        update_record('aformat_forum_posts', $afpRecord);
                    }
                    echo '</div>';
                    echo '</td>';
                    
                    echo '</tr>';
                }
            }
            $aforumposts = get_records_select('aformat_forum_posts',"themeid=$aformatforumthemeid AND postid=0", "$aftRecord->postssorting");
            $afmid = 0;
            foreach ($aforumposts as $afpRecord) {
                $userinfo = get_record('user', 'id', $afpRecord->userid);
                echo '<tr class="section separator"><td colspan="3" class="spacer"></td></tr>';
                echo '<tr class="section main">';
                
                echo '<td colspan="3" style="border:solid silver 1px" class="content">';

                echo '<table width="100%">';
                echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'"><td colspan="2">';
                echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/user/view.php?id='.$userinfo->id.'&course='.$course->id.'">'.$userinfo->firstname.' '.$userinfo->lastname.' '.$userinfo->idnumber.' ('.date('d/M/Y',$afpRecord->posted).')</a>';
                echo ' <a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatcollection='.$userinfo->id.'">(collection)</a>';
                echo '</td></tr>';
                echo '<tr><td>'.$afpRecord->post.'</td><td align="right" valign="middle" width="'.$aftRecord->postimagewidth.'px">';
                if (isset($afpRecord->image)&&($afpRecord->image!='')){
                    if (strtolower(substr($afpRecord->image, -3))=='flv'){
                        $afmid++;
                        $pluginId = 'filter_swf_'.time().$afpRecord->id;
                        echo '<span class="mediaplugin mediaplugin_flv" id="'.$pluginId.'">('.get_string('flashvideo', 'mediaplugin').')</span>
                                <script type="text/javascript">
                                //<![CDATA[
                                  var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afpRecord->userid.'/'.$afpRecord->id.'/'.$afpRecord->image.'",
                                    width:"'.$aftRecord->postimagewidth.'", height:"'.$aftRecord->postimageheight.'", majorversion:"6", build:"40",
                                    allowscriptaccess:"never", quality: "high", allowfullscreen: "true" };
                                  UFO.create(FO, "'.$pluginId.'");
                                //]]>
                                </script>';
                    } elseif (strtolower(substr($afpRecord->image, -3))=='avi') {
                        $url=$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afpRecord->userid.'/'.$afpRecord->id.'/'.$afpRecord->image;
                        $mpsize = 'width="'.$aftRecord->postimagewidth.'" height="'.$aftRecord->postimageheight.'"';
                        $autosize = 'false';
                        $autostart = 'false';
                        echo '<span class="mediaplugin mediaplugin_wmp">
                                <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
                                  standby="Loading Microsoft(R) Windows(R) Media Player components..."
                                  type="application/x-oleobject">
                                 <param name="Filename" value="'.$url.'" />
                                 <param name="src" value="'.$url.'" />
                                 <param name="url" value="'.$url.'" />
                                 <param name="ShowControls" value="true" />
                                 <param name="AutoRewind" value="true" />
                                 <param name="AutoStart" value="'.$autostart.'" />
                                 <param name="Autosize" value="'.$autosize.'" />
                                 <param name="EnableContextMenu" value="true" />
                                 <param name="TransparentAtStart" value="false" />
                                 <param name="AnimationAtStart" value="false" />
                                 <param name="ShowGotoBar" value="false" />
                                 <param name="EnableFullScreenControls" value="true" />
                                <!--[if !IE]>-->
                                  <object data="'.$url.'" type="'.$mimetype.'" '.$mpsize.'>
                                   <param name="src" value="'.$url.'" />
                                   <param name="controller" value="true" />
                                   <param name="autoplay" value="'.$autostart.'" />
                                   <param name="autostart" value="'.$autostart.'" />
                                   <param name="resize" value="scale" />
                                  </object>
                                <!--<![endif]-->
                                </object></span>';
                    } elseif (strtolower(substr($afpRecord->image, -3))=='jvx') {
                        $url=$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afpRecord->userid.'/'.$afpRecord->id.'/'.$afpRecord->image;
                        $pluginId = 'filter_jvx_'.time().$afpRecord->id;
                        echo '<div>
                            <APPLET name=jvLite code=jvLite.class
                                                      width='.$aftRecord->postimagewidth.' height='.$aftRecord->postimageheight.'
                                                      alt="JavaView lite applet"
                                                      archive="'.$CFG->wwwroot.'/filter/jvbm/jars/jvLite.jar" id='.$pluginId.'>
                                                      <PARAM NAME="model" VALUE="'.$url.'">
                            </APPLET>
                            </div>
                            ';
                    } else {
                        echo '<img border="0" width="'.$aftRecord->postimagewidth.'" height="'.$aftRecord->postimageheight.'" src="'.$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afpRecord->userid.'/'.$afpRecord->id.'/'.$afpRecord->image.'" />';
                    }
                }
                echo '</td></tr>';
                echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'"><td align="right" colspan="2">';
                
                if (isloggedin()&&isstudent($course->id)&&!$aftConcluded||isediting($course->id)) {
                    echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afpRecord->themeid.'&aformatforumpostpost=true">post</a>';
                    echo ' | ';
                    echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afpRecord->themeid.'&aformatforumpostpost=true&aformatforumpostreply='.$afpRecord->id.'">reply</a>';
                    if ($USER->id==$userinfo->id||isediting($course->id)) {
                        echo ' | ';
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afpRecord->themeid.'&aformatforumposteditid='.$afpRecord->id.'">edit</a>';
                        echo ' | ';
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afpRecord->themeid.'&aformatforumpostdelid='.$afpRecord->id.'">delete</a>';
                    }                    
                }

                echo '</td></tr>';
                
                $aforumpostreplies = get_records('aformat_forum_posts','postid', $afpRecord->id, "$aftRecord->postssorting");
                $afmid = 0;
                foreach ($aforumpostreplies as $afprRecord) {
                    $userinfo = get_record('user', 'id', $afprRecord->userid);
                    echo '<tr><td colspan="2">&nbsp</td></tr>';
                    echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'"><td colspan="2">';
                    echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/user/view.php?id='.$userinfo->id.'&course='.$course->id.'">'.$userinfo->firstname.' '.$userinfo->lastname.' '.$userinfo->idnumber.' ('.date('d/M/Y',$afprRecord->posted).')</a>';
                    echo ' <a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatcollection='.$userinfo->id.'">(collection)</a>';
                    echo '</td></tr>';
                    echo '<tr><td>'.$afprRecord->post.'</td><td align="right" valign="middle" width="'.$aftRecord->postimagewidth.'px">';
                    if (isset($afprRecord->image)&&($afprRecord->image!='')){
                        if (strtolower(substr($afprRecord->image, -3))=='flv'){
                            $afmid++;
                            $pluginId = 'filter_swf_'.time().$afprRecord->id;
                            echo '<span class="mediaplugin mediaplugin_flv" id="'.$pluginId.'">('.get_string('flashvideo', 'mediaplugin').')</span>
                                    <script type="text/javascript">
                                    //<![CDATA[
                                      var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afprRecord->userid.'/'.$afprRecord->id.'/'.$afprRecord->image.'",
                                        width:"'.$aftRecord->postimagewidth.'", height:"'.$aftRecord->postimageheight.'", majorversion:"6", build:"40",
                                        allowscriptaccess:"never", quality: "high", allowfullscreen: "true" };
                                      UFO.create(FO, "'.$pluginId.'");
                                    //]]>
                                    </script>';
                            } elseif (strtolower(substr($afprRecord->image, -3))=='avi') {
                                $url=$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afprRecord->userid.'/'.$afprRecord->id.'/'.$afprRecord->image;
                                $mpsize = 'width="'.$aftRecord->postimagewidth.'" height="'.$aftRecord->postimagewidth.'"';
                                $autosize = 'false';
                                $autostart = 'false';
                                echo '<span class="mediaplugin mediaplugin_wmp">
                                        <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
                                          standby="Loading Microsoft(R) Windows(R) Media Player components..."
                                          type="application/x-oleobject">
                                         <param name="Filename" value="'.$url.'" />
                                         <param name="src" value="'.$url.'" />
                                         <param name="url" value="'.$url.'" />
                                         <param name="ShowControls" value="true" />
                                         <param name="AutoRewind" value="true" />
                                         <param name="AutoStart" value="'.$autostart.'" />
                                         <param name="Autosize" value="'.$autosize.'" />
                                         <param name="EnableContextMenu" value="true" />
                                         <param name="TransparentAtStart" value="false" />
                                         <param name="AnimationAtStart" value="false" />
                                         <param name="ShowGotoBar" value="false" />
                                         <param name="EnableFullScreenControls" value="true" />
                                        <!--[if !IE]>-->
                                          <object data="'.$url.'" type="'.$mimetype.'" '.$mpsize.'>
                                           <param name="src" value="'.$url.'" />
                                           <param name="controller" value="true" />
                                           <param name="autoplay" value="'.$autostart.'" />
                                           <param name="autostart" value="'.$autostart.'" />
                                           <param name="resize" value="scale" />
                                          </object>
                                        <!--<![endif]-->
                                        </object></span>';
                            } elseif (strtolower(substr($afpRecord->image, -3))=='jvx') {
                                $url=$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afpRecord->userid.'/'.$afpRecord->id.'/'.$afpRecord->image;
                                $pluginId = 'filter_jvx_'.time().$afpRecord->id;
                                echo '<div>
                                    <APPLET name=jvLite code=jvLite.class
                                                              width='.$aftRecord->postimagewidth.' height='.$aftRecord->postimageheight.'
                                                              alt="JavaView lite applet"
                                                              archive="'.$CFG->wwwroot.'/filter/jvbm/jars/jvLite.jar" id='.$pluginId.'>
                                                              <PARAM NAME="model" VALUE="'.$url.'">
                                    </APPLET>
                                    </div>
                                    ';
                            } else {
                                echo '<img border="0" width="'.$aftRecord->postimagewidth.'" height="'.$aftRecord->postimageheight.'" src="'.$CFG->wwwroot.'/file.php/'.$course->id.'/aformatdata/'.$afprRecord->userid.'/'.$afprRecord->id.'/'.$afprRecord->image.'" />';
                            }
                        }
                    echo '</td></tr>';
                    echo '<tr style="background-color: '.$aftRecord->subjectbgcolor.'; color: '.$aftRecord->subjectforecolor.'"><td align="right" colspan="2">';

                    if (isloggedin()&&isstudent($course->id)&&!$aftConcluded||isediting($course->id)) {
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afprRecord->themeid.'&aformatforumpostpost=true">post</a>';
                        echo ' | ';
                        echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afpRecord->themeid.'&aformatforumpostpost=true&aformatforumpostreply='.$afpRecord->id.'">reply</a>';
                        if ($USER->id==$userinfo->id||isediting($course->id)) {
                            echo ' | ';
                            echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afprRecord->themeid.'&aformatforumposteditid='.$afprRecord->id.'">edit</a>';
                            echo ' | ';
                            echo '<a style="color: '.$aftRecord->subjectforecolor.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'&aformatforumthemeid='.$afprRecord->themeid.'&aformatforumpostdelid='.$afprRecord->id.'">delete</a>';
                        }
                    }

                    echo '</td></tr>';
                }
                echo '</table>';
                
                echo '</td>';
                
                echo '</tr>';
            }
        }
    }


    echo '</table>';

    if (!empty($sectionmenu)) {
        echo '<div class="jumpmenu">';
        echo popup_form($CFG->wwwroot.'/course/view.php?id='.$course->id.'&amp;', $sectionmenu,
                   'sectionmenu', '', get_string('jumpto'), '', '', true);
        echo '</div>';
    }

    print_container_end();

    echo '</td>';

            break;
            case 'right':
    // The right column
    if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $editing) {
        echo '<td style="width: '.$preferred_width_right.'px;" id="right-column">';

        print_container_start();
        blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
        print_container_end();

        echo '</td>';
    }

            break;
        }
    }
    echo '</tr></table>';

?>